<template>
  <div class="sign-box">


    <el-row>
      <el-breadcrumb>
        <el-breadcrumb-item>主页</el-breadcrumb-item>
        <el-breadcrumb-item>我的签到</el-breadcrumb-item>
        <el-breadcrumb-item>签到管理</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>
    <el-row>搜索区</el-row>
    <el-row>

      <el-button-group>
        <el-button type="success" @click="btnAddClick">添加</el-button>

      </el-button-group>
      <el-table :data="data.list">
        <el-table-column prop="title" label="签到主题"></el-table-column>
        <el-table-column prop="teacherName" label="发起教师"></el-table-column>
        <el-table-column prop="studentName" label="学生姓名"></el-table-column>
        <el-table-column
            prop="signTime"
            label="签到时间"
            :formatter="FormatTime"
        >

        </el-table-column>

        <el-table-column>
          <template #default="scope">
            <el-button type="warning" @click="btnEditClick(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination layout="prev, pager, next" :total="1000"></el-pagination>
    </el-row>
  </div>


  <el-dialog v-model="data.info.show">
    <template #title>
      <h1>{{ data.info.title }}</h1>
    </template>

    <el-form label-width="90px">
      <el-form-item label="签到主题">
        <el-select v-model="data.info.stuSign.signId">
          <el-option v-for="sign in data.signs" :value="sign.signId" :label="sign.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学生">
        <el-select v-model="data.info.stuSign.studentId">
          <el-option v-for="stu in data.students" :value="stu.studentId" :label="stu.name"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="btnSubmitSave">提交</el-button>
      </el-form-item>


    </el-form>
  </el-dialog>
</template>

<script lang="ts" setup>

import {onMounted, reactive, ref} from "vue";
import {ElMessage} from "element-plus";
import axios from "axios";
import monent from "moment";
import router from "@/router";

const studentId = sessionStorage.getItem("stuid");

const data = reactive({
  list: [],
  info: {
    show: false,
    title: '',
    stuSign: {
      studentSignId:-1,
      title: '',
      signId: -1,
      studentId: -1
    }
  },
  students:new Array<Object>(), // ts数组 ，需要明确指出放到数组中的数据类型
  signs:new Array<Object>()
})





let setDefault = () => {

  data.info.stuSign.title = '';
  data.info.stuSign.studentId = -1
  data.info.stuSign.signId = -1
}
let btnAddClick = () => {
  setDefault();
  data.info.title = "添加签到"
  data.info.show = true;
}
let btnEditClick = (row: any) => {

  setDefault();
  data.info.title = "修改签到"
  data.info.stuSign.studentSignId= row.studentSignId;
  data.info.stuSign.title = row.title;
  data.info.stuSign.studentId = row.studentId;
  data.info.stuSign.signId = row.signId;
  data.info.show = true
}

let FormatTime = (row: any, column: any) => {
  var date = row[column.property];  //获取传过来的时间
  if (date == undefined) {          //判断时间是否为空
    return "";                      //如果为空就返回一个空的字符串
  }
  return monent(date).format("YYYY-MM-DD HH:mm:ss");
  //如果值不为空，就把传过来的那个时间，按格式修改，然后返回
}


let btnSubmitSave = () => {

  axios({
    url: 'http://localhost:8080/ssign/save',
    params: data.info.stuSign,
    method: 'post'
  }).then((res) => {
    if (res.data.success) {
      ElMessage.info({
        message:'更新成功',
        duration:1500
      })
      initTable(); // 添加或者修改成功，需要重新更新列表
      data.info.show = false;
    }
  })
}





function initTable() {
  axios({
    url: 'http://localhost:8080/ssign/bystuid',
    method: 'get',
    params: {
      studentId: studentId
    }
  }).then((res) => {
    if (res.data.success) {
      data.list = res.data.data;
      console.log(res.data.success);
    } else {
      router.push("/login")
    }

  })
}

let initStudents = () => {
  axios({
    url: 'http://localhost:8080/stu/list',
    method: 'get'
  }).then((res) => {

     data.students = res.data.data;

    data.students.unshift({
      studentId: -1,
      name: '全体学生'
    })

  })


}

let initSigns = () => {
  axios({
    url: 'http://localhost:8080/signin/list',
    method: 'get'
  }).then((res)=>{

    data.signs =res.data.data;
    data.signs.unshift({
      signId:-1,
      name:'所有签到'
    })

  })
}

onMounted(() => {

  initTable();
   initSigns();
   initStudents();

})




</script>

<style scoped>
.sign-box {
  padding: 10px;
}
</style>